/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.windfly.don.filter;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
//import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;

import cn.windfly.don.DonConfig;

/**
 *
 * @author windfly
 */
@WebFilter(urlPatterns={"*.js","*.css","*.jpg","*.gif","*.png","*.icon"})
public class ExpiresFilter implements Filter {
    private String expires;
    private long nextDeploymentTime;
 
    public ExpiresFilter() {
        expires = nextDeploymentTime();
    }
 
    private String nextDeploymentTime() {
        // assume next deployment is M-F at 09:45
        Calendar c = Calendar.getInstance();
        //默认缓存1天
        int dayOffset = 1;
         
        //周五+2天
        if (c.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) {
            dayOffset += 2;
        }
        //周六+1天
        if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
            dayOffset++;
        }
 
        c.add(Calendar.DAY_OF_MONTH, dayOffset);
        c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH),
                c.get(Calendar.DAY_OF_MONTH), 9, 45);
 
        nextDeploymentTime = c.getTimeInMillis();
 
        String pattern = "EEE, dd MMM yyyy HH:mm:ss z";
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
        return sdf.format(c.getTime());
    }
 
    private void addCacheHeaders(ServletRequest request, ServletResponse response)
            throws IOException, ServletException {
 
        HttpServletResponse sr = (HttpServletResponse) response;
        sr.setHeader("Expires", expires);
        long now = (new Date()).getTime();
 
        long expireTime = nextDeploymentTime - now;
        expireTime /= 1000;
        sr.setHeader("Cache-Control", "max-age="
                + Long.toString(expireTime) + ";public;must-revalidate;");
    }
 
    /**
     *
     * @param request The servlet request we are processing
     * @param response The servlet response we are creating
     * @param chain The filter chain we are processing
     *
     * @exception IOException if an input/output error occurs
     * @exception ServletException if a servlet error occurs
     */
    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain)
            throws IOException, ServletException {
 
    	if (DonConfig.getBoolean("don.cookie.img.expires")) {
    		addCacheHeaders(request, response);
		}
        chain.doFilter(request, response);
    }

    @Override
    public void init(FilterConfig fc) throws ServletException {
    }

    @Override
    public void destroy() {
    }
}
